/*
 * GTrigger.java
 *
 * Created on 17 maart 2008, 21:09
 *
 * @author wijnand.schepens@hogent.be
 */
package model.graphic.objects;

import controller.commands.ClearSelectionCommand;
import controller.commands.ExecuteCommand;
import controller.commands.SetSelectedCommand;
import core.Tracker;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import model.graphic.locations.FlexibleRelativeLocation;
import view.GUI.sidepanels.CustomArgument;
import view.GUI.graphic.GraphicView;

public class GTrigger extends AbstractHandle {

    private int radius = 5; //default

    public GTrigger() {
    }

    public GTrigger(Attachment baseHandle, int radius) {
        super(baseHandle, new FlexibleRelativeLocation(((RectangleAttachment) baseHandle).location, -radius, 15 / 2));
        this.radius = radius;
    }

    public void move(Attachment base) {
        setBase(base);
        adjustLocation(base, new FlexibleRelativeLocation(((RectangleAttachment) base).location, -radius, 15 / 2));
    }

    public void paint(Graphics2D g) {
        if (this.isVisible() && (base.isVisible() || ((RectangleAttachment)base).isCollapsed() )  && ((Attachment)base).getBase().isVisible()) {
            Color oldColor = g.getColor();
            g.setColor(getBackColor());

            RectangleAttachment rh = (RectangleAttachment) base;
            g.fillOval(rh.location.getX() - getRadius() - getRadius(), rh.location.getY() - getRadius() + 15 / 2, 2 * getRadius(), 2 * getRadius());

            if (selected) {
                g.setColor(getHighlightColor());
            } else {
                g.setColor(getBaseColor());
            }

            g.drawOval(rh.location.getX() - getRadius() - getRadius(), rh.location.getY() - getRadius() + 15 / 2, 2 * getRadius(), 2 * getRadius());
            if (highlighted) {
                g.setColor(getHighlightColor());
                g.drawOval(rh.location.getX() - getRadius() - getRadius(), rh.location.getY() - getRadius() + 15 / 2, 2 * getRadius(), 2 * getRadius());
                g.drawOval(rh.location.getX() - getRadius() - getRadius() - 2, rh.location.getY() - getRadius() + 15 / 2 - 2, 2 * (getRadius() + 2), 2 * (getRadius() + 2));
            }
            g.setColor(oldColor);
        }
    }

    @Override
    public boolean contains(int x, int y) {
        return ((((RectangleAttachment) base).location.getX() - getX() - getRadius()) - x) * ((((RectangleAttachment) base).location.getX() - getX() - getRadius()) - x) + (((RectangleAttachment) base).location.getY() - getY() + 15 / 2 - y) * (((RectangleAttachment) base).location.getY() - getY() + 15 / 2 - y) <= getRadius() * getRadius();
    }

    /**
     * @return the radius
     */
    public int getRadius() {
        return radius;
    }

    /**
     * @param radius the radius to set
     */
    public void setRadius(int radius) {
        this.radius = radius;
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        GraphicView gv = (GraphicView) this.getParent();
        if (e.isPopupTrigger()) {
            gv.notifyObservers(new ClearSelectionCommand());
            gv.notifyObservers(new SetSelectedCommand(this, true));
            gv.getPopup().menuTrigger(this);
            gv.getPopup().getPopup().show(this, e.getX(), e.getY());
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        GraphicView gv = (GraphicView) this.getParent();
        if (!e.isControlDown()) {
            gv.notifyObservers(new ClearSelectionCommand());
            gv.notifyObservers(new SetSelectedCommand(this, true));
            gv.notifyObservers(new CustomArgument("tabRight", 0));
        } else {
            if (!this.isSelected()) {
                gv.notifyObservers(new SetSelectedCommand(this, true));
                gv.notifyObservers(new CustomArgument("tabRight", 0));
            } else {
                gv.notifyObservers(new SetSelectedCommand(this, false));
            }
        }
        if (e.getClickCount() == 2) {
            Tracker.setInstance(new Tracker());
            gv.notifyObservers(new ExecuteCommand(this));
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        this.setHighlighted(true);
        GraphicView gv = (GraphicView) this.getParent();
        gv.setTriggerFound(this);
        gv.repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        this.setHighlighted(true);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        this.setHighlighted(false);
        GraphicView gv = (GraphicView) this.getParent();
        gv.setTriggerFound(null);
        gv.repaint();
    }
}
